package com.djhu.newscollector.crawel.net;

import java.io.UnsupportedEncodingException;

/**
 * Created by zw on 2018/2/11.
 */
public class HttpResponse {

    private HttpRequest request;

    private String html;

    private byte[] bytes;

    private int code;

    private String charset;

    public HttpRequest getRequest() {
        return request;
    }

    public void setRequest(HttpRequest request) {
        this.request = request;
    }

    public String getHtml() {

        if (html != null) {
            return html;
        }

        if (bytes == null) {
            return null;
        }
        if (charset == null) {
            charset = CharsetDetector.guessEncoding(bytes);
        }
        try {
            html = new String(bytes, charset);
        } catch (UnsupportedEncodingException e) {
            //LOG.info("Exception when decoding "+ key(),e);
            return null;
        }

        return html;
    }

    public void setHtml(String html) {
        this.html = html;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}